package com.brkj.test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import com.brkj.httpInterface.BaseAjaxParams;
import com.brkj.httpInterface.HttpInterface;
import com.brkj.main.R;
import com.brkj.model.DS_Vote;
import com.brkj.util.MyAjaxCallBack;
import com.brkj.util.MyApplication;
import com.brkj.util.view.BaseCoreActivity;
import com.brkj.util.view.BaseListFragment;
import com.brkj.util.view.BaseListView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import net.tsz.afinal.FinalHttp;

import java.lang.reflect.Type;
import java.util.List;

/**
 * Created by colin.ding on 2014/5/7.
 */
public class VoteView extends BaseListView {

    private BroadcastReceiver br;
    private List<DS_Vote> voteList;
    protected VoteAdapter adapter;

    public VoteView(Context context) {
        super(context);
    }

    public void show(){
        if((hasShown)&& (voteList.size()>0)){
            return;
        }else{
            fillView();
            hasShown = true;
        }

    }

    @Override
    protected void fillView() {
        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetVoteList.params.UserID, MyApplication.myUserID);

        FinalHttp fh = new FinalHttp();
        fh.post(HttpInterface.HIF_GetVoteList.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)mContext) {


            @Override
            public void onSuccess(Object t) {
                // TODO Auto-generated method stub
                super.onSuccess(t);

                Gson gson = new Gson();
                Type listType = new TypeToken<List<DS_Vote>>() {
                }.getType();
                voteList = gson.fromJson((String) t, listType);

                if ((voteList != null) && (voteList.size() > 0)) {
                    adapter = new VoteAdapter(mContext, voteList);
                    listView.setAdapter(adapter);
//                    setListAdapter(adapter);

                     noContent.setVisibility(View.GONE);
                } else {
                    noContent.setVisibility(View.VISIBLE);
                }
            }

        });
    }

//
//    @Override
//    public void onListItemClick(ListView l, View v, int position, long id) {
//        super.onListItemClick(l, v, position, id);
//    }
}